home *** CD-ROM | disk | FTP | other *** search
- #include "main.h"
- #include "dialogs.h"
-
- static int myVol;
-
- /*******************************************************
- * *
- * Gets info needed for later, when we may be dumping source code, and we need *
- * to open our own data fork to get the file from. There does not seem to be any *
- * good way of finding out the volume the file is in at any time but startup. *
- * *
- *******************************************************/
-
- initDialogs() {
- int errno;
- char tempStr[ pasStrLen ];
-
- if( errno = GetVol( tempStr, &myVol )) hardPanic( errno, diskPanic );
- }
-
- /*******************************************************
- * *
- * Filter handles most normal filtering actions required for a dialog. It assumes *
- * that the refCon of the dialog contains a refConHandle, and calls the filterProc *
- * which this identifies to take care of any extra filtering specific to the dialog. *
- * *
- *******************************************************/
-
- pascal int filter( theDialog, theEvent, itemHit )
- DialogPtr theDialog;
- EventRecord *theEvent;
- int *itemHit;
- {
- refConHandle theRefConHand;
- refConPtr theRefCon;
- int result = 0;
- char theChar;
-
- doBackground( nothingElseToDo, noGray, NULL );
- if( theRefConHand = ( refConHandle )GetWRefCon( theDialog )) {
- HLock( theRefConHand );
- theRefCon = *theRefConHand;
- if( theRefCon->filter )
- ( *( theRefCon->filter ))( theDialog, theEvent, itemHit, &( theRefCon->refCon ));
- HUnlock( theRefConHand );
- }
- switch( theEvent->what )
- {
- case updateEvt:
- if( ( DialogPtr )( theEvent->message ) == theDialog )
- {
- BeginUpdate( theDialog );
- drawDefault( theDialog );
- DrawDialog( theDialog );
- EndUpdate( theDialog );
- }
- else doUpdate( theEvent->message );
- theEvent->what = nullEvent;
- break;
- case keyDown:
- theChar = theEvent->message & charCodeMask;
- if( *itemHit = findButton( theDialog, theChar, theEvent->modifiers ))
- result = -1;
- break;
- default: ;
- }
- return( result );
- }
-
- /*******************************************************
- * *
- * FixButtons updates all of the controls of a dialog so that they have a trivial *
- * tracking routine. Obviously this needs to be fixed if I ever need to do any *
- * special tracking in a dialog. *
- * *
- *******************************************************/
-
- pascal doNothingAction( theControl, partCode )
- ControlHandle theControl;
- int partCode;
- {
- doBackground( someElseToDo, noGray, NULL );
- }
-
- fixButtons( theDialog )
- WindowPeek theDialog;
- {
- ControlHandle theItem;
-
- theItem = theDialog->controlList;
- while( theItem && *theItem )
- {
- SetCtlAction( theItem, doNothingAction );
- theItem = (**theItem).nextControl;
- }
- }
-
- /*******************************************************
- * *
- * This draws an outline around the default button of the given dialog. It is used *
- * in response to update events coming through the filterProc. *
- * *
- *******************************************************/
-
- drawDefault( theDialog )
- DialogPtr theDialog;
- {
- ControlHandle theItem;
- GrafPtr savePort;
- Rect theRect;
- PenState savePen;
- int type;
-
- GetPort( &savePort );
- SetPort( theDialog );
- GetPenState( &savePen );
- GetDItem( theDialog, (( DialogPeek )theDialog )->aDefItem, &type, &theItem, &theRect );
- PenMode( patOr );
- PenPat( black );
- PenSize( 3, 3 );
- InsetRect( &theRect, -4, -4 );
- FrameRoundRect( &theRect, 16, 16 );
- SetPenState( &savePen );
- SetPort( savePort );
- }
-
- /*******************************************************
- * *
- * CenterWindow is simple - it centers the given window horizontally on the main *
- * screen. It isn't at all smart about multi-gdevice systems. *
- * *
- *******************************************************/
-
- centerWindow( theWindow )
- WindowPtr theWindow;
- {
- int newleft;
-
- newleft = ( screenBits.bounds.right + screenBits.bounds.left
- - theWindow->portRect.right - theWindow->portRect.left ) / 2;
- MoveWindow( theWindow, newleft, theWindow->portRect.top - theWindow->portBits.bounds.top, 0 );
- }
-
- /*******************************************************
- * *
- * doDialog handles all of the housekeeping for bringing up a dialog box. It loads *
- * the dialog with the given ID, initializes fields appropriately, and runs the *
- * dialog until it exits. Its filterProc calls the one given on every event, with the *
- * address of the stored refCon field, as well as the usual parameters. *
- * *
- *******************************************************/
-
- doDialog( id, hitProc, filterProc, refCon )
- int id;
- int (*hitProc)(), (*filterProc)();
- long refCon;
- {
- refConHandle theRefCon;
- WindowPeek theWindow;
- DialogPtr theDialog;
- DialogTemplate **tempHand, *theTemp;
- Handle items;
- int itemHit, isDA;
-
- theWindow = ( WindowPeek )FrontWindow();
- isDA = theWindow && theWindow->windowKind < 0;
- if( isDA ) setBadScrap();
-
- tempHand = ( DialogTemplate** )GetResource( 'DLOG', id );
- HLock( tempHand );
- theTemp = *tempHand;
- items = GetResource( 'DITL', theTemp->itemsID );
- HandToHand( &items );
- theDialog = NewDialog( NULL, &( theTemp->boundsRect ), &( theTemp->title ), 0, theTemp->procID,
- -1L, theTemp->goAwayFlag, theTemp->refCon, items );
- HUnlock( tempHand );
- theRefCon = ( refConHandle )NewHandle(( long )sizeof( refConData ));
- ( **theRefCon ).filter = filterProc;
- ( **theRefCon ).refCon = refCon;
- SetWRefCon( theDialog, theRefCon );
- fixButtons( theDialog );
- centerWindow( theDialog );
- ShowWindow( theDialog );
- ModalDialog( filter, &itemHit );
- while( itemHit != 1 ) {
- if( hitProc ) ( *hitProc )( theDialog, itemHit );
- ModalDialog( filter, &itemHit );
- }
- DisposHandle( theRefCon );
- DisposDialog( theDialog );
- if( isDA ) exportScrap();
- }
-
- /*******************************************************
- * *
- * The following routines handle the about box. *
- * *
- * AboutFilter makes sure that the free memory count shown in the about box *
- * is up to date. *
- * *
- *******************************************************/
-
- aboutFilter( theDialog, theEvent, itemHit, theRefCon )
- DialogPtr theDialog;
- EventRecord *theEvent;
- int *itemHit;
- long *theRefCon;
- {
- ControlHandle item;
- Rect box;
- long newFree;
- int type;
- char *num, memFree[ numStrLen ];
-
- newFree = FreeMem();
- if( newFree != *theRefCon )
- {
- *theRefCon = newFree;
- displayNumPas( newFree, memFree + numStrLen - 1, &num );
- GetDItem( theDialog, 4, &type, &item, &box );
- SetIText( item, num );
- }
- }
-
- /*******************************************************
- * *
- * AboutHit will only be called when the source button is pressed, so it need not *
- * pay attention to its parameters. *
- * *
- * DumpSource requests a filename to deposit the source code into, and if one is *
- * given it calls copyFromDataFork to copy the source code (compressed) out of *
- * the data fork of the application into the named file. The default and prompt *
- * strings should be stored as resources. *
- * *
- *******************************************************/
-
- aboutHit( theDialog, itemHit )
- DialogPtr theDialog;
- int itemHit;
- {
- dumpSource();
- }
-
- dumpSource()
- {
- static Point where = { 70, 100 };
- static char prompt[25] = "\pDestination Filename:",
- destName[25] = "\pLife Source.sit";
- SFReply reply;
-
- installModalTrap();
- SFPutFile( where, prompt, destName, NULL, &reply );
- removeModalTrap();
- if( reply.good ) copyFromDataFork( reply.fName, reply.vRefNum );
- }
-
- /*******************************************************
- * *
- * CopyFromDataFork does just that - given a filename and volume number it *
- * copies the content of the application's data fork into the named file. It deletes *
- * the file if it is there already, and tries to create it if it is not. *
- * *
- *******************************************************/
-
- copyFromDataFork( name, dest )
- char *name;
- int dest;
- {
- Handle apParam;
- long count = buf_len;
- int dref, sref, bad = -1, myResFile;
- char myName[ pasStrLen ], buffer[ buf_len ];
-
- FSDelete( name, dest );
- if( Create( name, dest, 'SIT!', 'SIT!' )) return( -1 );
- if( !FSOpen( name, dest, &dref ))
- {
- GetAppParms( myName, &myResFile, &apParam );
- if( !FSOpen( myName, myVol, &sref ))
- {
- while( !FSRead( sref, &count, buffer ))
- FSWrite( dref, &count, buffer );
- FSWrite( dref, &count, buffer );
- FSClose( sref );
- bad = 0;
- }
- FSClose( dref );
- }
- if( bad ) FSDelete( name, dest );
- FlushVol( NULL, dest );
- }
-
- /*******************************************************
- * *
- * DoAbout is deceptively simple - it simply calls up the about box, with the *
- * above routines as the hit and filter routines. Note that the above filter is *
- * called by the actual dialog filter routine, which takes care of such things as *
- * mapping keys to buttons, handling updates, and calling doBackground. *
- * *
- *******************************************************/
-
- doAbout()
- {
- long free;
- char memFree[13], *num;
-
- free = FreeMem();
- displayNumPas( free, memFree + 12, &num );
- ParamText( num, NULL, NULL, NULL );
-
- doDialog( aboutDlg, aboutHit, aboutFilter, free );
- }
-
- /*******************************************************
- * *
- * What to do when we run into trouble. Routines here for 'panic' cases, when we *
- * can't do something that we have committed to doing already. *
- * *
- *******************************************************/
-
- hardPanic( errno, which )
- int errno, which;
- {
- softPanic( errno, which );
- ExitToShell();
- }
-
- softPanic( errno, which )
- int errno, which;
- {
- char theString[ pasStrLen ], errNoStr[ numStrLen ], *num;
-
- GetIndString( theString, panicStrs, which );
- displayNumPas(( long )which, errNoStr + numStrLen - 1, &num );
- ParamText( theString, num, NULL, NULL );
- doDialog( panicDlg, NULL, NULL, NULL );
- }